/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.entities;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Winner
 */
public class CartBean {

    private List<TblOrderDetail> cart;

    public CartBean() {
        cart = new ArrayList<TblOrderDetail>();
    }

    public void addItem(TblElevation tblElevation, int oddQuantity) {
        boolean newItem = true;

        for (TblOrderDetail item : cart) {

            if (item.getTblElevation().getElID() == tblElevation.getElID()) {
                newItem = false;
                item.setOddQuantity(oddQuantity);
            }
        }

        if (newItem) {
            TblOrderDetail scItem = new TblOrderDetail();
            scItem.setTblElevation(tblElevation);
            scItem.setOddQuantity(oddQuantity);
            cart.add(scItem);
        }
    }

    public void addItem(TblElevation tblElevation) {
        boolean newItem = true;

        for (TblOrderDetail item : cart) {

            if (item.getTblElevation().getElID() == tblElevation.getElID()) {
                newItem = false;
                item.setOddQuantity(item.getOddQuantity() + 1);
            }
        }

        if (newItem) {
            TblOrderDetail scItem = new TblOrderDetail();
            scItem.setTblElevation(tblElevation);
            scItem.setOddQuantity(1);
            cart.add(scItem);
        }
    }

    public double calculatePrice(TblElevation tblElevation) {
        for (TblOrderDetail item : cart) {
            if (item.getTblElevation().getElID() == tblElevation.getElID()) {
                return (item.getTblElevation().getElCost() * item.getOddQuantity());
            }
        }
        return 0;
    }

    public double calculateTotal() {
        float total = 0;
        for (TblOrderDetail item : cart) {
            total += (item.getTblElevation().getElCost() * item.getOddQuantity());
        }
        return total;
    }

    public synchronized void delete(TblOrderDetail od) {
        cart.remove(od);
    }

    public synchronized List<TblOrderDetail> getCart() {
        return cart;
    }

    public synchronized void setCart(List<TblOrderDetail> items) {
        this.cart = items;
    }

    public synchronized void clear() {
        cart.clear();
    }
}
